package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.xgkj.diyiketang.EatShowApp;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.DetailFansAdapter;
import com.xgkj.diyiketang.activity.data.TieZiAdapter;
import com.xgkj.diyiketang.data.ClubInfo;
import com.xgkj.diyiketang.data.ContentInfo;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.task.ApplySheTuanTask;
import com.xgkj.diyiketang.task.ClubContentListTask;
import com.xgkj.diyiketang.task.ClubDetailTask;
import com.xgkj.diyiketang.task.ClubMemberListTask;
import com.xgkj.diyiketang.util.ToastUtil;
import com.xgkj.diyiketang.weidget.MyCustomListView;
import com.xgkj.diyiketang.weidget.MyScrollView;
import com.xgkj.diyiketang.weidget.NoScrollGridView;

import java.util.List;

/**
 * Created by XCKJ_Paladowow on 2016/7/28.
 */
public class SheTuanDetailActivity extends BaseActivity implements View.OnClickListener {
    private Context mContext;
    private MyCustomListView mGridView;
    private TieZiAdapter mAdapter;
    private RelativeLayout mTopLayout;
    private TextView back;
    private GridView fansGrid;
    private DetailFansAdapter dtfansAdapter;
    private Button moreFans;
    private String clubId;
    private int clubDetailTaskId = -1;
    private ClubInfo clubinfo;
    private TextView detailText;
    private TextView timeText;
    private TextView memberText;
    private int fanListTaskId = -1;
    private LinearLayout mFansLayout;
    private int currentPage = 1;
    private int tieziListTaskId = -1;
    private String contentId;
    private MyScrollView myScrollView;
    private String isclub;
    private TextView titleText;
    private String titleString;
    private ImageView fatiezi;
    private ImageView applyST;
    private int applystTaskId= -1;
    private String creatorId;
    private ImageView newApply;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shetuan_detail);
        mContext = this;
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        if (intent != null) {
            clubId = intent.getStringExtra("CLUBID");
            titleString= intent.getStringExtra("NAME");
        }
        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mTopLayout = (RelativeLayout) findViewById(R.id.toplayout);
        mTopLayout.setFocusable(true);
        mTopLayout.setFocusableInTouchMode(true);
        mTopLayout.requestFocus();
        myScrollView = (MyScrollView) findViewById(R.id.myscroll);
        myScrollView.setOnScrollListener(new MyScrollView.OnScrollListener() {
            @Override
            public void onScroll() {
                getTieZiList();
            }
        });

        mGridView = (MyCustomListView) findViewById(R.id.tiezi_list);
        mAdapter = new TieZiAdapter(mContext);
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ContentInfo info = (ContentInfo) mAdapter.getItem(position);
                contentId = info.getContentId();
                Intent intent = new Intent(mContext, TieZiDetailActivity.class);
                intent.putExtra("ID",contentId);
                startActivity(intent);
            }
        });
        mGridView.setAdapter(mAdapter);

        moreFans = (Button) findViewById(R.id.more_fans_list);
        moreFans.setOnClickListener(this);

        mFansLayout = (LinearLayout) findViewById(R.id.fanslist_layout);
        fansGrid = (GridView) findViewById(R.id.fans_list);
        dtfansAdapter = new DetailFansAdapter(mContext);
        fansGrid.setAdapter(dtfansAdapter);

        detailText = (TextView) findViewById(R.id.club_detail);
        timeText = (TextView) findViewById(R.id.club_time);
        memberText = (TextView) findViewById(R.id.club_number);
        titleText = (TextView) findViewById(R.id.title);
        titleText.setText(titleString);
        fatiezi = (ImageView) findViewById(R.id.fatiezi);
        fatiezi.setOnClickListener(this);
        applyST = (ImageView) findViewById(R.id.apply_st);
        applyST.setOnClickListener(this);

        newApply = (ImageView) findViewById(R.id.newapply);
        newApply.setOnClickListener(this);

        getCludDetail();
    }

    private void getCludDetail() {
        ClubDetailTask clubTask = new ClubDetailTask(clubId);
        clubTask.addListener(new ClubDetailTaskListener(mContext));
        clubDetailTaskId = getEatShowApp().getTaskManager().addTask(clubTask);
    }

    private void getAnchorFansList() {
        ClubMemberListTask fansTask = new ClubMemberListTask(5, 1, clubId);
        fansTask.addListener(new ClubDetailTaskListener(mContext));
        fanListTaskId = getEatShowApp().getTaskManager().addTask(fansTask);
    }

    private void getTieZiList() {
        if (currentPage == 0) {
//              Toast.makeText(mContext, R.string.no_more, Toast.LENGTH_SHORT).show();
            mGridView.setNoNextPage();
            return;
        }
        ClubContentListTask tieziTask = new ClubContentListTask(10, currentPage, clubId);
        tieziTask.addListener(new ClubDetailTaskListener(mContext));
        tieziListTaskId = getEatShowApp().getTaskManager().addTask(tieziTask);
    }

    private void doApplySheTuan(){
        String userid = EatShowApp.getInstance().getmUserInfo().getUserId();
        ApplySheTuanTask aTask = new ApplySheTuanTask(userid,clubId);
        aTask.addListener(new ClubDetailTaskListener(mContext));
        applystTaskId = getEatShowApp().getTaskManager().addTask(aTask);
    }

    class ClubDetailTaskListener extends ShowToastTaskEventListener {

        public ClubDetailTaskListener(Context context) {
            super(context);
        }

        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (taskId == clubDetailTaskId) {
                clubinfo = (ClubInfo) data;
                detailText.setText(clubinfo.getClubDesc());
                timeText.setText(clubinfo.getCreateDate());
                memberText.setText(clubinfo.getMemberCount());
                isclub = clubinfo.getIsClub();
                creatorId = clubinfo.getClubCreator();
                if(isclub.equals("1")){
                    fatiezi.setVisibility(View.GONE);
                    applyST.setVisibility(View.VISIBLE);
                    newApply.setVisibility(View.GONE);
                }else {
                    fatiezi.setVisibility(View.VISIBLE);
                    applyST.setVisibility(View.GONE);
                    newApply.setVisibility(View.GONE);
                }

                String userId = EatShowApp.getInstance().getmUserInfo().getUserId();
                if(userId.equals(creatorId)){
                    applyST.setVisibility(View.GONE);
                    newApply.setVisibility(View.VISIBLE);
                }else {
                   applyST.setVisibility(View.VISIBLE);
                    newApply.setVisibility(View.GONE);
                }

//                getAnchorFansList();
            } else if (taskId == fanListTaskId) {
                if (data != null) {
                    List<EatShowUserInfo> list = (List<EatShowUserInfo>) data;
                    if (list != null && list.size() > 0) {
                        dtfansAdapter.updateData(list);
//                        fansListDivider.setVisibility(View.VISIBLE);
                    } else {
                        mFansLayout.setVisibility(View.GONE);
//                        fansListDivider.setVisibility(View.GONE);
                    }
                    getTieZiList();
                }
            } else if (taskId == tieziListTaskId) {
                if (data != null) {
                    List<ContentInfo> clist = (List<ContentInfo>) data;
                    if (clist != null && clist.size() > 0) {
                        mAdapter.updateData(clist);
                    }
                    currentPage = result;
                }
            }else if(taskId == applystTaskId){
                ToastUtil.showMessage(mContext,"提交申请成功，请等待管理员审核！");
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        dtfansAdapter.clearData();
        currentPage = 1;
        mAdapter.clearData();
        getAnchorFansList();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.more_fans_list:
                Intent intent = new Intent(mContext, STMemberListActivity.class);
                intent.putExtra("ID",clubId);
                intent.putExtra("CRID",creatorId);
                intent.putExtra("ISCLUB",isclub);
                startActivity(intent);
                break;

            case R.id.apply_st:
                doApplySheTuan();
                break;

            case R.id.newapply:
                Intent aIntent = new Intent(mContext,STApplyListActivity.class);
                aIntent.putExtra("ID",clubId);
                startActivity(aIntent);
                break;
            case R.id.fatiezi:
                Intent tIntent = new Intent(mContext,FaTieZiActivity.class);
                tIntent.putExtra("CLUBID",clubId);
                startActivity(tIntent);
                break;
        }
    }

//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        currentPage = 1;
//        getTieZiList();
//    }
}
